package com.toogu.mazhangmen.ui.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.toogu.mazhangmen.R;
import com.toogu.mazhangmen.util.UiUtil;

import org.w3c.dom.Text;

public class SearchView extends LinearLayout implements View.OnClickListener{

    private OnClickListener onClickListener;
    private EditText textView;

    public interface OnClickListener{
        void onSearchViewClick(String string);
    }

    public SearchView(Context context) {
        super(context);
        init(context);
    }

    public SearchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SearchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        setOrientation(HORIZONTAL);
        textView = new EditText(context);
        textView.setPadding(UiUtil.dip2px(10),0,UiUtil.dip2px(10),0);
        textView.setBackgroundColor(getResources().getColor(R.color.blue));
//        textView.setHint(R.string.main_searchView_hint);
        textView.setTextColor(Color.WHITE);
        textView.setHintTextColor(Color.GRAY);
        textView.setMaxLines(1);
        textView.setGravity(Gravity.CENTER|Gravity.START);

        LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.weight=1;
        addView(textView,layoutParams);

        layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.setMargins(UiUtil.dip2px(5),0,0,0);

        addView(createButton(context,"搜索",this),layoutParams);
    }

    private TextView createButton(Context activity,  String content, View.OnClickListener onClickListener) {
        TextView button =(TextView) LayoutInflater.from(activity).inflate(R.layout.view_control_button, null);
        button.setText(content);
        button.setPadding(UiUtil.dip2px(20),0,UiUtil.dip2px(20),0);
        button.setOnClickListener(this);
        return button;
    }

    public void setClear(){
        textView.setText("");
    }

    public void setOnSearchViewClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    @Override
    public void onClick(View v) {
      if(onClickListener!=null)  onClickListener.onSearchViewClick(textView.getText().toString());
    }
}
